window.onload = function() {

    // 隐藏发送验证码的按钮
    $(".over2").hide();

    var regtel = /^1[3|4|5|7|8]\d{9}$/; // 手机号码的正则表达式
    var regqq = /^[1-9]\d{4,}$/; // 10000
    var regnc = /^[a-zA-Z0-9]{2,8}$/;
    // var regmsg = /^\d{6}$/;
    var regmsg = /^\d{4}$/;
    var regpwd = /^[a-zA-Z0-9_-]{6,16}$/;
    var tel = document.querySelector('#tel');
    // var qq = document.querySelector('#qq');
    var nc = document.querySelector('#nc');
    var msg = document.querySelector('#msg');
    var pwd = document.querySelector('#pwd');
    var surepwd = document.querySelector('#surepwd');
    regexp(tel, regtel); // 手机号码
    // regexp(qq, regqq); // qq号码
    regexp(nc, regnc); // 昵称
    regexp(msg, regmsg); // 短信验证
    regexp(pwd, regpwd); // 密码框
    // 表单验证的函数
    function regexp(ele, reg) {
        ele.onblur = function() {
            if (reg.test(this.value)) {
                // console.log('正确的');
                this.nextElementSibling.className = 'success';
                this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您输入正确';
            } else {
                // console.log('不正确');
                this.nextElementSibling.className = 'error';
                this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 格式不正确，请从新输入 ';
            }

            // 控制手机号验证通过与否的发送验证码按钮是否显示
            if (this.id == 'tel') {
                // 当前验证手机号
                if (reg.test(this.value)) {
                    // 验证手机号通过
                    $(".over2").show();
                } else {
                    // 验证手机号不通过
                    $(".over2").hide();
                }
            }
        }
    };

    surepwd.onblur = function() {
        if (this.value == pwd.value) {
            this.nextElementSibling.className = 'success';
            this.nextElementSibling.innerHTML = '<i class="success_icon"></i> 恭喜您输入正确';
        } else {
            this.nextElementSibling.className = 'error';
            this.nextElementSibling.innerHTML = '<i class="error_icon"></i> 两次密码输入不一致';

        }
    }

    $(".over2").click(function(){
        console.log("发送验证码");
        $.getJSON("/api/sms/sendVerifyCode", {
            phone: $("#tel").val()
        }, ret => {
            console.log(ret);
            // 发送成功后需要将发送验证码的按钮设置成倒计时
            if (ret['code'] == 2001) {
                // 1.将uuid设置到表单的隐藏域
                $("input[name=uuid]").val(ret['data']['uuid']);
                // 2.控制按钮倒计时
                let allTime = 300;
                let fun = () => {
                    let strContent = '';
                    if (allTime > 0) {
                        allTime--;
                        strContent += `剩余${allTime}秒`;
                        $(".over2").prop('disabled', 'disabled');
                    } else {
                        strContent += `发送验证码`;
                        $(".over2").removeAttr('disabled');
                        clearInterval(timerId);
                    }
                    $(".over2").val(strContent);
                };
                fun();
                let timerId = setInterval(fun, 1000);
            }
        });
    });

    // 给注册表单绑定提交事件
    $("#registerForm").submit(() => {
        // 获取相应的注册参数
        let uuid = $("input[name=uuid]").val();
        let phone = $("#tel").val();
        let username = $("#nc").val();
        let code = $("#msg").val();
        let password = $("#pwd").val();
        // 加密
        password = md5(password);
        // 将对象转成json串
        /*
              "code": "9602",
              "password": "123",
              "phone": "15655706368",
              "username": "lisi",
              "uuid": "3204e5498b57448b94244f466d3c19d8"
         */
        let jsonRegister = JSON.stringify({uuid, code, phone , username,password});
        if ($("#agree").prop("checked")) {
            // 选中，可以注册
            // 发送注册请求
            $.ajax({
                url: "/api/user/register",
                type: "post",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: jsonRegister,
                success: function(result){
                    console.log(result);
                    // 注册成功后跳转到登录页面
                    alert('注册用户成功');
                    if (result['code'] == 1003) {
                        window.location.href = "/login.html";
                    }
                }
            });
        } else {
            // 没选中
            alert("您未同意协议，不能注册");
            return false;
        }
        return false;
    });
}